package com.fairytask.ui.item
{
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.define.CoolDefine;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.tools.SystemTime;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;

	/**
	 * 冷却的子项
	 * @author yangj
	 **/
	
	public class CoolDownItem extends Sprite
	{
		public var txtInfo 	: TextField;//冷却信息
		private var timeShow	: TimeShow;//时间显示
		public var endCoolBtn	: Button;//结束冷却按钮
		public var type:int;//类型
		public var totalCount	:int;	//总数
		
		//冷却类型
		private const CoolState_Free	:int	=	0;//空闲
		private const CoolState_Normal	:int	=	1;//正常
		private const CoolState_Force	:int	=	2;//强制		
		private const CoolState_Bad	:int	=	3;//不可用
		
		public function CoolDownItem(_type:int)
		{
			super();
			init(_type);
			initEvent();
		}
		
		private function init(_type:int):void
		{	
			type = _type;
			txtInfo = new TextField();
			txtInfo.width = 115;
			txtInfo.height= 18;
			txtInfo.selectable = false;
			txtInfo.x = 7;
			txtInfo.y = 4;
			addChild(txtInfo);
			
			timeShow = new TimeShow(0,"ffffff");
			timeShow.setTimer();
			timeShow.x = 90;
			timeShow.y = 4;
			addChild(timeShow);
			
			endCoolBtn = new Button("",18,15,"Button_0106");
			endCoolBtn.width = 18;
			endCoolBtn.height= 14;
			endCoolBtn.x = 158;
			endCoolBtn.y = 6;
			addChild(endCoolBtn);
		}
		
		private function initEvent():void
		{
			endCoolBtn.addEventListener(MouseEvent.CLICK,onClick);
			timeShow.addEventListener(TimerEvent.TIMER_COMPLETE,sendMessage);
		}
		
		private function sendMessage(evt:TimerEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_COOLDOWN_END);
			
		}
		
		private function onClick(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_COOLDOWN,type);
		}
		
		/**
		 * 队列项等待状态
		 * 
		 */		
		public function waitState():void
		{
			txtInfo.textColor = 0x00FF00;
			switch(type)
			{
				case CoolDefine.COOL_DOWN_CHALLENGE:
					txtInfo.text = StaticIni.getSwfLangStr("1000000266");
					break;
				case CoolDefine.COOL_DOWN_EQUIP:
					txtInfo.text = StaticIni.getSwfLangStr("1000000262");
					break;
				case CoolDefine.COOL_DOWN_EXTRACT:
					txtInfo.text = StaticIni.getSwfLangStr("1000000263");
					break;
				case CoolDefine.COOL_DOWN_HUNTER_LIFE:
					txtInfo.text = StaticIni.getSwfLangStr("1000000264");
					break;
				case CoolDefine.COOL_DOWN_UPGRADE_PET:
					txtInfo.text = StaticIni.getSwfLangStr("1000000265");
					break;
				case CoolDefine.COOL_DOWN_ADVANCE_SKILL:
					txtInfo.text = StaticIni.getSwfLangStr("1000000267");
					break;
			}
			timeShow.visible = false;
			endCoolBtn.visible = false;
		}
		
		/**
		 * 根据状态来设置颜色 
		 * @param pStatus
		 * 0x00FF00 绿色 0xFF0000 红色
		 */
		public function colorWithStatus(pStatus:int):void
		{
			switch(pStatus)
			{
				case CoolState_Free:
				case CoolState_Normal:
					txtInfo.textColor	=	0x00FF00;
					timeShow.styleColor("00FF00");
					break;
				case CoolState_Force:
				case CoolState_Bad:
					txtInfo.textColor	=	0xFF0000;
					timeShow.styleColor("FF0000");
					break;
			}
		}
		
		/**
		 * 队列项冷却状态 
		 * @param val
		 * 
		 */		
		public function coolState(val:int,startTime:int):void
		{
			//txtInfo.textColor = 0xFFFFFF;
			switch(type)
			{
				case CoolDefine.COOL_DOWN_CHALLENGE:
					txtInfo.text = StaticIni.getSwfLangStr("1000000271");
					break;
				case CoolDefine.COOL_DOWN_EQUIP:
					txtInfo.text = StaticIni.getSwfLangStr("1000000270");
					break;
				case CoolDefine.COOL_DOWN_ADVANCE_SKILL:
					txtInfo.text = StaticIni.getSwfLangStr("1000000272");
					break;
				case CoolDefine.COOL_DOWN_UPGRADE_PET:
					txtInfo.text = StaticIni.getSwfLangStr("1000000400");
					break;
			}
			var leaveTime : Number = SystemTime.getLeftTime(startTime*1000,val*1000);
			timeShow.visible = true;
			endCoolBtn.visible = true;
			timeShow.time = leaveTime/1000;
		}
		
		/**
		 * 返回冷却类型 
		 * @return 
		 * 
		 */
		public function get coolType():int
		{
			return type;
		}
		
		/**
		 * 返回剩余的冷却时间 
		 * @return 
		 * 
		 */
		public function get coolTime():int
		{
			if(timeShow.time>0)
			{
				return timeShow.time;
			}
			else
			{
				return 0;
			}
		}
		
		public function get timeShowTxt():TextField
		{
			return timeShow.timeTextField;
		}
	}
}